<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对 JSON 格式的字符串进行解码</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ref.json.html">« JSON 函数</a></li>
      <li style="float: right;"><a href="function.json-encode.html">json_encode »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.json.html">JSON 函数</a></li>
    <li>对 JSON 格式的字符串进行解码</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.json-decode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">json_decode</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8, PECL json &gt;= 1.2.0)</p><p class="refpurpose"><span class="refname">json_decode</span> &mdash; <span class="dc-title">对 JSON 格式的字符串进行解码</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.json-decode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>json_decode</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$json</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">bool</span><span class="type"></span></span> <code class="parameter">$associative</code><span class="initializer"> = <strong><code>null</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span><br>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   接受一个 JSON 编码的字符串并且把它转换为 PHP 值。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.json-decode-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">json</code></dt>

     <dd>

      <p class="para">
       待解码的 <code class="parameter">json</code> <span class="type">string</span> 格式的字符串。
      </p>
      <p class="para">
       这个函数仅能处理 UTF-8 编码的数据。
      </p>
      
 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
   PHP 实现了 JSON 的一个超集，参考
   <a href="http://www.faqs.org/rfcs/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a>.
  </p>
 </p></blockquote>

     </dd>

    
    
     <dt>
<code class="parameter">associative</code></dt>

     <dd>

      <p class="para">
       当为 <strong><code>true</code></strong> 时，JSON 对象将返回关联 <span class="type">array</span>；当为 <strong><code>false</code></strong> 时，JSON 对象将返回
       <span class="type">object</span>。当为 <strong><code>null</code></strong> 时，JSON 对象将返回关联 <span class="type">array</span> 或 <span class="type">object</span>，这取决于是否在
       <code class="parameter">flags</code> 中设置 <strong><code>JSON_OBJECT_AS_ARRAY</code></strong>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">depth</code></dt>

     <dd>

      <p class="para">
       需要解码的结构，其最大嵌套深度。该值必须大于 <code class="literal">0</code> 或者小于等于 <code class="literal">2147483647</code>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       由 <strong><code>JSON_BIGINT_AS_STRING</code></strong>、<strong><code>JSON_INVALID_UTF8_IGNORE</code></strong>、<strong><code>JSON_INVALID_UTF8_SUBSTITUTE</code></strong>、<strong><code>JSON_OBJECT_AS_ARRAY</code></strong>、<strong><code>JSON_THROW_ON_ERROR</code></strong>
       组成的掩码。这些常量的行为在 <a href="json.constants.html" class="link">JSON constants</a> 页面有进一步描述。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.json-decode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   通过恰当的 PHP 类型返回在 <code class="parameter">json</code>
   中编码的数据。值 <code class="literal">true</code>、<code class="literal">false</code> 和 <code class="literal">null</code>
   会相应地返回 <strong><code>true</code></strong>、<strong><code>false</code></strong> 和 <strong><code>null</code></strong>。如果 <code class="parameter">json</code> 
   无法被解码，或者编码数据深度超过了嵌套限制的话，将会返回 <strong><code>null</code></strong> 。
  </p>
 </div>

 
 <div class="refsect1 errors" id="refsect1-function.json-decode-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果 <code class="parameter">depth</code> 超出允许的范围，自 PHP 8.0.0 起将会抛出
   <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>，在此之前的版本将会引发
   <strong><code>E_WARNING</code></strong> 级别的错误。
  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-function.json-decode-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.3.0</td>
       <td>
        <code class="parameter">flags</code> 新增 <strong><code>JSON_THROW_ON_ERROR</code></strong>。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        现在 <code class="parameter">associative</code> 允许为 null。
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        <code class="parameter">flags</code> 新增 <strong><code>JSON_INVALID_UTF8_IGNORE</code></strong>、<strong><code>JSON_INVALID_UTF8_SUBSTITUTE</code></strong>。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        An empty JSON key (&quot;&quot;) can be encoded to the empty object property
        instead of using a key with value <code class="literal">_empty_</code>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.json-decode-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3409">
    <p><strong>示例 #1 <span class="function"><strong>json_decode()</strong></span> 的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"a":1,"b":2,"c":3,"d":4,"e":5}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (5) {
    [&quot;a&quot;] =&gt; int(1)
    [&quot;b&quot;] =&gt; int(2)
    [&quot;c&quot;] =&gt; int(3)
    [&quot;d&quot;] =&gt; int(4)
    [&quot;e&quot;] =&gt; int(5)
}

array(5) {
    [&quot;a&quot;] =&gt; int(1)
    [&quot;b&quot;] =&gt; int(2)
    [&quot;c&quot;] =&gt; int(3)
    [&quot;d&quot;] =&gt; int(4)
    [&quot;e&quot;] =&gt; int(5)
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3410">
    <p><strong>示例 #2 Accessing invalid object properties</strong></p>
    <div class="example-contents"><p>
     Accessing elements within an object that contain characters not
     permitted under PHP&#039;s naming convention (e.g. the hyphen) can be
     accomplished by encapsulating the element name within braces and the apostrophe.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"foo-bar":&nbsp;12345}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">);<br />print&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'foo-bar'</span><span style="color: #007700">};&nbsp;</span><span style="color: #FF8000">//&nbsp;12345<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-3411">
    <p><strong>示例 #3 common mistakes using <span class="function"><strong>json_decode()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;following&nbsp;strings&nbsp;are&nbsp;valid&nbsp;JavaScript&nbsp;but&nbsp;not&nbsp;valid&nbsp;JSON<br /><br />//&nbsp;the&nbsp;name&nbsp;and&nbsp;value&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br />//&nbsp;single&nbsp;quotes&nbsp;are&nbsp;not&nbsp;valid&nbsp;<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"{&nbsp;'bar':&nbsp;'baz'&nbsp;}"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br />//&nbsp;the&nbsp;name&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz"&nbsp;}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br />//&nbsp;trailing&nbsp;commas&nbsp;are&nbsp;not&nbsp;allowed<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz",&nbsp;}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-3412">
    <p><strong>示例 #4 <code class="parameter">depth</code> errors</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Encode&nbsp;some&nbsp;data&nbsp;with&nbsp;a&nbsp;maximum&nbsp;depth&nbsp;&nbsp;of&nbsp;4&nbsp;(array&nbsp;-&gt;&nbsp;array&nbsp;-&gt;&nbsp;array&nbsp;-&gt;&nbsp;string)<br /></span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'English'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'One'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'January'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'French'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Une'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Janvier'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br /><br /></span><span style="color: #FF8000">//&nbsp;Show&nbsp;the&nbsp;errors&nbsp;for&nbsp;different&nbsp;depths.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">'Last&nbsp;error:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_last_error_msg</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">'Last&nbsp;error:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">json_last_error_msg</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [1]=&gt;
  array(2) {
    [&quot;English&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;One&quot;
      [1]=&gt;
      string(7) &quot;January&quot;
    }
    [&quot;French&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;Une&quot;
      [1]=&gt;
      string(7) &quot;Janvier&quot;
    }
  }
}
Last error: No error

NULL
Last error: Maximum stack depth exceeded
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3413">
    <p><strong>示例 #5 <span class="function"><strong>json_decode()</strong></span> of large integers</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"number":&nbsp;12345678901234567890}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">512</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_BIGINT_AS_STRING</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (1) {
  [&quot;number&quot;]=&gt;
  float(1.2345678901235E+19)
}
object(stdClass)#1 (1) {
  [&quot;number&quot;]=&gt;
  string(20) &quot;12345678901234567890&quot;
}
</pre></div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.json-decode-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    The JSON spec is not JavaScript, but a subset of JavaScript.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    In the event of a failure to decode, <span class="function"><a href="function.json-last-error.html" class="function">json_last_error()</a></span>
    can be used to determine the exact nature of the error.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.json-decode-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.json-encode.html" class="function" rel="rdfs-seeAlso">json_encode()</a> - 对变量进行 JSON 编码</span></li>
    <li class="member"><span class="function"><a href="function.json-last-error.html" class="function" rel="rdfs-seeAlso">json_last_error()</a> - 返回最后发生的错误</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>